package com.etc.controller;

import com.etc.entity.Cinema;
import com.etc.entity.Movie;
import com.etc.service.MovieService;
import com.etc.util.CommonMessage;
import com.etc.util.DateUtil;
import com.etc.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2019/5/21.
 */
@Controller
public class MovieController {
    @Autowired
    private MovieService movieService;
    @Autowired
    private DateUtil dateUtil;

    @GetMapping(value = "home")
    public String listSoonMovie(Model model) {
        List<Movie> soonMovies = movieService.findByMstatus();
        List<Movie> hotMovies = movieService.findByMstatusOrderByMgradeDesc();
        List<Movie> topMovies = movieService.findByMstatusOrderByMgradeAsc();
        model.addAttribute("hotMovies", hotMovies);
        model.addAttribute("soonMovies", soonMovies);
        model.addAttribute("topMovies", topMovies);
        return "index";
    }

    @GetMapping(value = "buy/{mid}")
    public String buyMovie(@PathVariable(value = "mid") Integer mid, Model model) {
        HashMap hashMap = dateUtil.getDate();
        Movie movie = movieService.findByMid(mid);
        List<Movie> hotMovies = movieService.findByMstatusOrderByMgradeDesc();
        model.addAttribute("hotMovies", hotMovies);
        model.addAttribute("movie", movie);
        model.addAttribute("date", hashMap);
        return "cinemaselect";
    }

    @GetMapping(value = "showByDate")
    @ResponseBody
    public List<Cinema> showCinema(@RequestParam(value = "mid") Integer mid, @RequestParam(value = "playDate") String playDate) {

        System.out.println(movieService.findByMidAndMstartime(mid, playDate));
        return movieService.findByMidAndMstartime(mid, playDate);
    }

    @GetMapping(value = "movieList")
    @ResponseBody
    public PageData<Movie> listUser(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "limit", defaultValue = "10") Integer limit, @RequestParam(value = "content", required = false, defaultValue = "") String content) {
        PageData<Movie> pd = movieService.getPageData(page - 1, limit, content);
        pd.setCode(0);
        return pd;
    }

    @PutMapping(value = "movieUpdate")
    @ResponseBody
    public CommonMessage updateMovie(@RequestBody Movie movie) {
        boolean flag = movieService.updateMovie(movie);
        CommonMessage cm=new CommonMessage();
        cm.setCode(0);
        if (flag){
           cm.setMsg("修改成功");
        }else {
            cm.setMsg("修改失败");
        }
        return cm;
    }

    @DeleteMapping(value = "deleteMovie/{mid}")
    @ResponseBody
    public CommonMessage deleteMovie(@PathVariable("mid") Integer mid) {
        boolean flag = movieService.deleteMovie(mid);

        CommonMessage cm = new CommonMessage("删除成功");
        cm.setCode(0);
        return cm;
    }
    @PostMapping(value = "search")
    public String FindMovie(Model model,String name) {
        System.out.println(name);
        if(name == null){
            name="";
        }
        List<Movie> Findmovies = movieService.FindMoviceByNameForSearch(name);
        model.addAttribute("Findmovies", Findmovies);
        return "search";
    }

}
